﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Small.Html.Common;
using Small.Html.Entities;
using Small.Html.Options;

namespace Small.Html.UI
{
  public  static class HtmlDateBox
    {

      /// <summary>
      /// 日期框
      /// </summary>
      /// <typeparam name="TModel"></typeparam>
      /// <typeparam name="TProperty"></typeparam>
      /// <param name="helper"></param>
      /// <param name="expression">Func<T>表达式</param>
      /// <param name="id">ID</param>
      /// <param name="format">格式</param>
      /// <param name="htmlAttributes">Html属性</param>
      /// <returns></returns>
      public static MvcHtmlString DateBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string id, DateBoxOptions format, object htmlAttributes = null) 
      {
          //获取实体类中值
          HtmlModelItem htmlItem = HtmlModelHelper.GetProperties(helper, expression);
          return new MvcHtmlString(GenerateHtml(htmlItem, id, format, htmlAttributes));
      
      }

      /// <summary>
      /// 日期框
      /// </summary>
      /// <typeparam name="TModel"></typeparam>
      /// <typeparam name="TProperty"></typeparam>
      /// <param name="helper"></param>
      /// <param name="expression">Func<T>表达式</param>
      /// <param name="format">格式</param>
      /// <param name="id">ID</param>
      /// <param name="htmlAttributes"></param>
      /// <returns></returns>
      public static MvcHtmlString DateBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression,DateBoxOptions format, string id = null, object htmlAttributes = null)
      {

          return DateBoxFor(helper, expression, id, format, htmlAttributes);

      }

      public static MvcHtmlString DateBox(this HtmlHelper helper, string name, string id, string value, DateBoxOptions format, object htmlAttributes = null)
      {

          if (format == null) 
          {
              format = new DateBoxOptions();
          }
          StringBuilder builder = new StringBuilder();
          //获取实体类中值
          HtmlModelItem htmlItem = new HtmlModelItem { Name = name, Value = value };

          return new MvcHtmlString(GenerateHtml(htmlItem, id, format, htmlAttributes));

      }

      /// <summary>
      /// 日期框
      /// </summary>
      /// <param name="helper"></param>
      /// <param name="name">名称</param>
      /// <param name="id">ID</param>
      /// <param name="format">格式</param>
      /// <param name="htmlAttributes">Html属性</param>
      /// <returns></returns>
      public static MvcHtmlString DateBox(this HtmlHelper helper, string name, string id, DateBoxOptions format, object htmlAttributes = null)
      {
          
          return DateBox(helper, name, id,null, format, htmlAttributes);

      }
      /// <summary>
      /// 日期框
      /// </summary>
      /// <param name="helper"></param>
      /// <param name="name">名称</param>
      /// <param name="format">格式</param>
      /// <param name="value">值</param>
      /// <param name="htmlAttributes">Html属性</param>
      /// <returns></returns>
      public static MvcHtmlString DateBox(this HtmlHelper helper, string name,DateBoxOptions format, string value =null, object htmlAttributes = null)
      {

          return DateBox(helper, name, null, value, format, htmlAttributes);

      }
      /// <summary>
      /// 日期框
      /// </summary>
      /// <param name="helper"></param>
      /// <param name="name">名称</param>
      /// <param name="format">格式</param>
      /// <param name="value">值</param>
      /// <param name="htmlAttributes">属性</param>
      /// <returns></returns>

      public static MvcHtmlString DateBox(this HtmlHelper helper, string name,string id, object htmlAttributes = null)
      {

         
          return DateBox(helper, name, null, null, null, htmlAttributes);

      }

      
      private static string GenerateHtml(HtmlModelItem htmlItem, string id, DateBoxOptions format, object htmlAttributes) 
      {
          StringBuilder builder = new StringBuilder();

          string dataOptions = DataOptions(format);
          builder.Append(String.Format("<input type=\"text\" name=\"{0}\" class=\"{1}\"  id=\"{2}\" value=\"{3}\"   {4}='{5}'",
              htmlItem.Name, format.Class,
              String.IsNullOrEmpty(id) ? htmlItem.Name : id,
               htmlItem.Value,
               format.ValidFlag, dataOptions));

          builder.Append(" " + dataOptions);
          builder.Append(" " + format.GetMessage());
          builder.Append(BaseHelper.FormatGenerateAttribute(htmlAttributes));
          builder.Append(" />\r\n");
          return builder.ToString();
      }

      private static string DataOptions(DateBoxOptions format)
      {
          bool isHasUsedFlag = false;
          StringBuilder builder = new StringBuilder();
          builder.Append("data-options=\"");

          //是否必填
          if (format.Required)
          {
              if (format.Required)
              {
                  builder.Append("," + format.GetRequired());
                  isHasUsedFlag = true;
              }
          }

          if (!String.IsNullOrEmpty(format.Formatter)) 
          {
              builder.Append(string.Format(",formatter:{0}", format.Formatter));
              isHasUsedFlag = true;
          }
          if (!String.IsNullOrEmpty(format.Parser)) 
          {
              builder.Append(string.Format(",parser:{1}", format.Parser));
              isHasUsedFlag = true;
          }

          //是否可见
          if (format.Disabled)
          {
              builder.Append("," + format.GetDisabled());
              isHasUsedFlag = true;
          }
          builder.Append("\"");

          if (isHasUsedFlag)
          {
              return BaseHelper.SubSymbal(builder);
          }
          else 
          {
              return null;
          }
      }
    }
}
